package com.gome.ocean.common.constants;

import java.io.File;

/**
 * datax 常量
 * 
 * @author haqiaolong 2016年1月21日 下午3:03:32
 * @since 5.0.0
 */
public class DataxContants {

    /**
     * datax 默认值
     */
    public static final int     DEFAULT_CHANNEL_SIZE           = 1;
    public static final int     DEFAULT_BYTE_SIZE              = 1048576;

    /**
     * datax 模板文件路径
     */
    public static final String  DEFAULT_DATAX_TEMPLATE         = "/conf/vm";
    /**
     * 默认地址
     */
    private static final String DATAX_HOME_ENV                 = "DATAX_HOME";
    private static final String DATAX_HOME_PROPERTY            = "datax.home.dir";
    private static final String DEFAULT_DATAX_HOME             = System.getProperty(DATAX_HOME_PROPERTY,
                                                                   System.getenv(DATAX_HOME_ENV));
    private static final String DEFAULT_BIN                    = "/bin";
    private static final String DEFAULT_JOB                    = "/job";
    private static final String DEFAULT_CONF                   = "/conf";
    private static final String DAFAULT_LOG                    = "/log";
    private static final String DEFAULT_LOG_PERF               = "/log_perf";
    private static final String DEAULT_CONF_SUFFIX             = ".json";
    private static final String DEFAULT_PYTHON_COMMOND         = "python";
    private static final String DEFAULT_DATAX_PYTHON           = "datax.py";
    private static final String DEFUALT_LOG_FILENAME           = "context?jobId=%d";
    public static final String  DEFAULT_DATAX_ERROR            = "com.alibaba.datax.common.exception.DataXException";
    private static final String DEFAULT_DATAX_CONSOLE_LOG_PATH = "http://%s/datax-console/api/log?historyJobId=%d&beginIndex=%d";
    public static final String  DATAX_API_SERVICE              = "http://%s/datax-console/api/context";
    private static final String DEFAULT_DATAX_INTERRUPT_PATH   = "http://%s/datax-console/api/job/interruptJob?id=%d";
    private static final String KILL_COMMAND                   = "ps -ef|grep %s|grep -v grep|cut -c 9-15|xargs kill -9";
    
     
    private static final String TDDL_GROUP_PREFIX="com.gome.gddl.jdbc.group_%s_%s";
    
    
    public static String getTddlGroupPrefix(String version,String dbGroupKey){
    	return String.format(TDDL_GROUP_PREFIX,version,dbGroupKey);
    }
    
    public static String getDataxConsoleLogPath(String ipaddr, Long historyJobId, Long beginIndex) {
        return String.format(DEFAULT_DATAX_CONSOLE_LOG_PATH, ipaddr, historyJobId, beginIndex);
    }

    public static String getDataxInterruptPath(final String service, final Long id) {
        return String.format(DEFAULT_DATAX_INTERRUPT_PATH, service, id);
    }

    public static String getKillTaskCommand(final long jsonName) {
        return String.format(KILL_COMMAND, "jobId=".concat(jsonName + ""));
    }

    public static String getDataxBinPath() {
        return DEFAULT_DATAX_HOME.concat(DEFAULT_BIN);
    }

    public static String getDataxConfPath() {
        return DEFAULT_DATAX_HOME.concat(DEFAULT_CONF);
    }

    public static String getDataxLogPath() {
        return DEFAULT_DATAX_HOME.concat(DAFAULT_LOG);
    }

    public static String getDataxLogPerfPath() {
        return DEFAULT_DATAX_HOME.concat(DEFAULT_LOG_PERF);
    }

    public static String getWriterPath(final long fileName) {
        return DEFAULT_DATAX_HOME.concat(DEFAULT_JOB)
            .concat(File.separator)
            .concat(fileName + "")
            .concat(DEAULT_CONF_SUFFIX);
    }

    public static String getFullFileName(final long fileName) {
        return String.format(DEFUALT_LOG_FILENAME, fileName);
    }

    public static String getServiceContextPath(final String ip) {
        return String.format(DATAX_API_SERVICE, ip);
    }

    public static String getPythonCommondHttpPath(final long jsonName, final String service) {
        return DEFAULT_PYTHON_COMMOND.concat(" ")
            .concat(getDataxBinPath())
            .concat(File.separator)
            .concat(DEFAULT_DATAX_PYTHON)
            .concat(" ")
            .concat(service)
            .concat("?jobId=")
            .concat(jsonName + "");
    }
}
